app.controller("z-visaWaiver-c", [ '$scope','$rootScope','$z-http','$sce','z-employee-s','z-dictionary-s','z-corporation-s','z-department-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,DictionaryService,CorporationService,DepartmentService,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		$scope.viewModel.employeeNo=null;
		$scope.viewModel.roleName=null;
		$scope.viewModel.departmentName=null;
		$scope.viewModel.corporationName=null;
		$scope.viewModel.level="";
		$scope.viewModel.checkAll=false;
		$scope.viewModel.isVisaWaiver="";

		$scope.editModel = {};
		$scope.viewModel.checkboxDataList=[];
		
	
		$scope.viewModel.employeeIsSeparated=null;
		$scope.viewModel.regionType=null;
		$scope.viewModel.employeeNo=null;
		$scope.viewModel.roleName=null;
		$scope.viewModel.departmentList=[];
		$scope.viewModel.corporationList=[];
		$scope.viewModel.employeeList=[];
		$scope.viewModel.level=null;
		$scope.viewModel.effectiveDate = new Date();
		// 定义生效时间框内的值，用于检索
		$scope.viewModel.changeEffectiveDate = new Date();
		//下拉多选框长度
		$scope.multiSelectMaxHeight=300;
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.effectiveDateOpen = false;
		$scope.viewModel.effectiveDateOpnened = false;
		$scope.viewModel.effectiveDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.effectiveDateOpnened = !$scope.viewModel.effectiveDateOpnened;

			
		};
		//加载信息
		$scope.viewModel.bindSelectRegionTypeData = function(){
			//查询公司
			CorporationService.getCorporationList({
				fromDate:$scope.viewModel.changeEffectiveDate,
			},
			function(data){
				var site = [];  
		        $.each(data, function(index, values) {  
		            site.push({  
		                label : "("+values.corporationCode+")"+values.corporationName,  
		                value : values.corporationCode  
		            });  
		        });  
		        $('#vCorporation').multiselect('dataprovider',site);
			},function(error) {
				
			});
		};
		$scope.viewModel.bindSelectRegionTypeData();
		
	
		/*******使用为下拉带勾选控件*********/
		//员工级别
		$('#vlevel').multiselect({
			includeSelectAllOption: true,
			enableFiltering: true,
			delimiterText: '; ',
            maxHeight: $scope.multiSelectMaxHeight
        });
		//公司
		$('#vCorporation').multiselect({
			includeSelectAllOption: true,
			enableFiltering: true,
			delimiterText: '; ',
            maxHeight: $scope.multiSelectMaxHeight
        });
		//公司选中事件
		$("#vCorporation").change(function(){  
			var value=$(this).val();
		    var data={};
		    data.corporationCodeList=value;
		    data.fromDate = $scope.viewModel.changeEffectiveDate;
		    if(value==null){
		    	var site = [];  
		    	$('#vDepartmentName').multiselect('dataprovider',site);
		    	$scope.viewModel.departmentList=[];
		    }else{
		    	//查询部门
			    DepartmentService.getDepartmentListByCorporationCodeList(data,
				function(data){
			    	var site = [];  
			        $.each(data, function(index, values) {  
			            site.push({  
			                label : "("+values.departmentCode+")"+values.departmentName,  
			                value : values.departmentCode  
			            });  
			        });  
			        $('#vDepartmentName').multiselect('dataprovider',site);
				},function(error) {
					
				});
		    }
        });  
		
		//部门
		$('#vDepartmentName').multiselect({
			includeSelectAllOption: true,
			enableFiltering: true,
			delimiterText: '; ',
            maxHeight: $scope.multiSelectMaxHeight
        });
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'visaWaiver'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载考勤状态数据
		$scope.viewModel.bindData = function(changeEffectiveDate){
			$scope.viewModel.employeeList=[];
			if($scope.viewModel.employeeNo!=null){
				var employeeNoData = $scope.viewModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewModel.employeeList.push(employeeNoData[i]);
				}
			}
			if(changeEffectiveDate == 1){
				$scope.editModel.changeEffectiveDate = $scope.viewModel.effectiveDate;
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getEmployeeList({
				employeeList:$scope.viewModel.employeeList,
				roleName:$scope.viewModel.roleName,
				departmentList:$scope.viewModel.departmentList,
				corporationList:$scope.viewModel.corporationList,
				employeeLevelList:$scope.viewModel.employeeLevelList,
				employeeIsSeparated:"0",
				perPage:$scope.viewModel.rowCount,
				rowCount:$scope.viewModel.perPage,
				fromDate:$scope.viewModel.effectiveDate,
				isVisaWaiver:$scope.viewModel.isVisaWaiver
			},
			function(data){
				$scope.viewModel.dataList = data.employeeBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.addCheckbox();
			    $scope.viewModel.JudgeCheckboxAll();
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindData();
		
		$scope.viewModel.save=function(){
			var employeeBean={};
			var data=[];
			for(var i=0;i<$scope.viewModel.dataList.length;i++){
				if($scope.viewModel.dataList[i].checkbox){
					 data.push($scope.viewModel.dataList[i].employeeId);
				 }
			};
			if(data.length<=0){
				$rootScope.alertWarning("未设置免签人员！");
				return;
			}
			$rootScope.alertConfirm("确定添加免签吗？",function() {
				employeeBean.employeeIdList=data;
				employeeBean.isType=true;
				$scope.viewModel.checkAll=false;
				$scope.viewModel.isLoading=true;
				Service.updateVisaWaiver(employeeBean,
					function(){
						$rootScope.alertInfo("添加免签成功。");
						$scope.viewModel.bindData();
						$scope.viewModel.isLoading=false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading=false;
					});
			});
		};
		$scope.viewModel.del=function(){
			var employeeBean={};
			var data=[];
			for(var i=0;i<$scope.viewModel.dataList.length;i++){
				if($scope.viewModel.dataList[i].checkbox){
					 data.push($scope.viewModel.dataList[i].employeeId);
				 }
			};
			if(data.length<=0){
				$rootScope.alertWarning("未设置免签人员！");
				return;
			}
			$rootScope.alertConfirm("确定删除免签吗？",function() {
				employeeBean.employeeIdList=data;
				employeeBean.isType=false;
				$scope.viewModel.isLoading=true;
				$scope.viewModel.checkAll=false;
				Service.updateVisaWaiver(employeeBean,
					function(){
						$rootScope.alertInfo("删除免签成功。");
						$scope.viewModel.bindData();
						$scope.viewModel.isLoading=false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading=false;
					});
			});
		};
		
		//是否选中
		$scope.viewModel.chkChecked=function(employee){
			if(employee.isVisaWaiver==1){
				return true;
			}
			return false;
		};
		
		//设置默认复选框值
		$scope.viewModel.addCheckbox=function(){
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 $scope.viewModel.dataList[i].checkbox=false;
			 }
		};
		
		//判断是否为全选
		$scope.viewModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 if($scope.viewModel.dataList[i].checkbox==false){
					 type=false;
					 break;
				 }else{
					 type=true;
				 }
			 }
			 if(type){
				 $scope.viewModel.checkAll=true;
			 }else{
				 $scope.viewModel.checkAll=false;
			 }
		};
		
		//全选事件
		$scope.viewModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.viewModel.checkAll==false){
				 angular.forEach($scope.viewModel.dataList, function(value, key) {
					 value.checkbox=true;
				 });
			 }else{
				 angular.forEach($scope.viewModel.dataList, function(value, key) {
					 value.checkbox=false;
				 });
			 }
		};
		
		//单选事件
		$scope.viewModel.checkbox=function(employee){
			 if(employee.checkbox){
				 employee.checkbox=false;
			 }else{
				 employee.checkbox=true;
			 }
			 $scope.viewModel.JudgeCheckboxAll();
		};
		
} ]);